package com.zjitc.pigspring.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.zjitc.pigspring.bean.po.Type;
import com.zjitc.pigspring.bean.po.User;
import com.zjitc.pigspring.common.Codes;
import com.zjitc.pigspring.common.Result;
import com.zjitc.pigspring.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private CollectService collectService;

    @Autowired
    private AddressService addressService;

    @Autowired
    private OrderService orderService;

    @Autowired
    private ShopService shopService;

    @GetMapping("/getAll")
    public Result<List<User>> getAll(){
        List<User> users = userService.list();
        if(users!=null){
            return Result.success(users);
        }else{
            return Result.error(Codes.GET_INFO_FAILED);
        }
    }

    @PostMapping("/addOne")
    public Result addUser(@RequestBody User user){
        QueryWrapper<User> wrapper = new QueryWrapper<User>()
                .eq("username",user.getUsername());
        User one = userService.getOne(wrapper);
        if (one!=null){
            return Result.error(Codes.USER_EXISTS);
        }
        boolean save = userService.save(user);
        if (save){
            return Result.success(Codes.ADD_YES);
        }else{
            return Result.error(Codes.ADD_NO);
        }
    }

    @DeleteMapping("/deleteOne/{id}")
    public Result deleteUser(@PathVariable Integer id){
        boolean flag = userService.removeById(id);
        boolean flag1 = deleteDataByUserId(id);
        if (flag && flag1){
            return Result.success(Codes.DELETE_YES);
        }else{
            return Result.error(Codes.DELETE_NO);
        }
    }

    private boolean deleteDataByUserId(Integer id) {

        return (collectService.deleteByUserId(id) && addressService.deleteByUserId(id) && shopService.deleteByUserId(id) && orderService.deleteByUserId(id));
    }

    @PostMapping("/deleteAll")
    public Result deleteUsers(@RequestBody List<Integer> ids){

        boolean flag = userService.removeBatchByIds(ids);
        boolean flag1 = true;
        for (Integer id : ids) {
            flag1 = deleteDataByUserId(id);
            if (!flag1){
                break;
            }
        }
        if (flag && flag1){
            return Result.success(Codes.DELETE_YES);
        }else{
            return Result.error(Codes.DELETE_NO);
        }
    }

    @GetMapping("/selectOne/{id}")
    public Result<User> selectUserById(@PathVariable Integer id){
        User user = userService.getById(id);
        if(user!=null){
            return Result.success(user);
        }else{
            return Result.error(Codes.GET_INFO_FAILED);
        }
    }

    @PutMapping("/updateOne")
    public Result updateUser(@RequestBody User user){
        QueryWrapper<User> wrapper = new QueryWrapper<User>()
                .eq("username",user.getUsername());
        User one = userService.getOne(wrapper);
        if (one!=null && !one.getUsername().equals(user.getUsername())){
            return Result.error(Codes.USER_EXISTS);
        }
        boolean flag = userService.updateById(user);
        if (flag){
            return Result.success(Codes.UPDATE_YES);
        }else{
            return Result.error(Codes.UPDATE_NO);
        }
    }

    @PostMapping("/conditionQuery")
    public Result<List<User>> queryType(@RequestBody User user){

        QueryWrapper<User> queryWrapper = new QueryWrapper<User>()
                .eq(StringUtils.isNotBlank(user.getName()),"name",user.getName());
        List<User> list = userService.list(queryWrapper);
        if(list!=null){
            return Result.success(list);
        }else{
            return Result.error(Codes.GET_INFO_FAILED);
        }
    }

}
